package com.demo.androidgame.samgukji_hit;

import java.util.ArrayList;
import java.util.List;

import android.graphics.PointF;
import android.util.Log;

import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class HeroStage {

	public static final float WORLD_WIDTH = 800 * 2;
	public static final float WORLD_HEIGHT = 480;
	public final Hero hero;
	public S_Arrow arrow;
	Camera2D cam;
	public static final int WORLD_STATE_RUNNING = 0;
	public static final int WORLD_STATE_NEXT_LEVEL = 1;
	public static final int WORLD_STATE_GAME_OVER = 2;
	//public static final Vector2 gravity = new Vector2(0, -12); // 중력 : 아래쪽으로 당기는
	public List<S_Arrow> arrows;
	
	public int state;
	public float Arrow_X, Arrow_Y;
	public float Arrow_move;

	
	public HeroStage() {
		hero = new Hero(400, 240);
		
		arrow = new S_Arrow(Arrow_X, Arrow_Y);
		this.arrows = new ArrayList<S_Arrow>();
		addArrow(Arrow_X, Arrow_Y);
		Arrow_X = 20;
		Arrow_Y = 0;
		Arrow_move = 5;
		this.state = WORLD_STATE_RUNNING;
	}

	

	public void update(float deltaTime) {

		// cam.position.x = hero.position.x;
		updateHero(deltaTime);
		updateBackground(deltaTime);
		
		arrow.update(deltaTime);
		if (arrow.state == S_Arrow.STATE_ARROW_FIRE_LEFT || arrow.state == S_Arrow.STATE_ARROW_FIRE_RIGHT) {
			updateArrows(deltaTime);
		}
	}

	private void updateBackground(float deltaTime) {
		if (hero.position.x <= 400) {
			Assets.heroStageBackgroundRegion.reset(0, 0, 800, 480);
		} else if (hero.position.x >= 1200) {
			Assets.heroStageBackgroundRegion.reset(800, 0, 800, 480);
		} else {
		Assets.heroStageBackgroundRegion.reset(hero.position.x - 400, 0, 800, 480);
		}
	}

	private void updateHero(float deltaTime) {
//	    if (hero.state != Hero.Hero_state_fire && hero.position.y <= 0.5f)
	    
	    if ( _dragging ){
	    	//hero.heroMove();
			hero.position.y += deltaX;
			hero.position.x += deltaY;
//			state = Hero.Hero_state_move;
//			hero.stateTime = 0;
//			hero.stateTime += deltaTime;
			Log.i("delta position", deltaX +  " / " + deltaY + " / " + hero.position.x + " / " + hero.position.y);
	    }
	    
	    
	  //if (hero.state != Hero.Hero_state_fire)
	  //    hero.velocity.x = -accelX / 30 * Hero.Hero_MOVE_VELOCITY;		//x값의 변화를 줌
	    hero.update(deltaTime);
	}
	
	public void addArrow(float x, float y) {
		arrow = new S_Arrow(x, y);
		
		arrows.add(arrow);
	}
	
	private void updateArrows(float deltaTime) {
		int len = arrows.size();
		for (int i = 0; i < len; i++) {
			S_Arrow arrow = arrows.get(i);
			if(arrow.state == S_Arrow.STATE_ARROW_FIRE_LEFT) {
				Arrow_move = -5;
			}
			if(arrow.state == S_Arrow.STATE_ARROW_FIRE_RIGHT) {
				Arrow_move = 5;
			}
	
			
			arrow.update(deltaTime);
		}
		
	}
	
	private TouchEvent lastEvent;
	public boolean _dragging;
	private PointF _touchingPoint = new PointF();
	float deltaX, deltaY;
	float deltaTime;
	Vector2 touchPoint = new Vector2();
	
	public void updateJoystick(TouchEvent event){

		if (event == null && lastEvent == null) {
			return;
		} else if(event == null && lastEvent != null){
			event = lastEvent;
		}else{
			lastEvent = event;
		}
		
		touchPoint.set(event.x, event.y);
		
		cam.touchToWorld(touchPoint);
	
		float x = cam.position.x - 345;
		float y = cam.position.y - 185;
		
		
		Rectangle r = new Rectangle(x - 45, y - 45, 90, 90);
		
		//drag drop 
		if ( event.type == TouchEvent.TOUCH_DOWN ){
			if (OverlapTester.pointInRectangle(r, touchPoint)) {
				_dragging = true;
				hero.heroMove();
			}
			
		}else if ( event.type == TouchEvent.TOUCH_UP){
			_dragging = false;
			touchPoint.x = cam.position.x - 345;
			touchPoint.y = cam.position.y - 185;
			hero.heroStop();
		} 

		if ( _dragging ){
	    	
			// get the pos
			_touchingPoint.x = touchPoint.x;
			_touchingPoint.y = touchPoint.y;
			
			// bound to a box
			if( _touchingPoint.x < x - 45){
				_touchingPoint.x = x - 45;
			}
			if ( _touchingPoint.x > x + 45){
				_touchingPoint.x = x + 45;
			}
			if (_touchingPoint.y < y - 45){
				_touchingPoint.y = y - 45;
			}
			if ( _touchingPoint.y > y + 45){
				_touchingPoint.y = y + 45;
			}

			//get the angle
			double angle = Math.atan2(_touchingPoint.y - y,_touchingPoint.x - x)/(Math.PI/180);
			
			// Move the beetle in proportion to how far 
			// the joystick is dragged from its center
			deltaX = (float)Math.sin(angle*(Math.PI/180)) * 5;//*(_touchingPoint.x/70);
			deltaY = (float)Math.cos(angle*(Math.PI/180)) * 5;//*(_touchingPoint.x/70);
			//hero.position.y += deltaX;
			//hero.position.x += deltaY;
			
			//Log.i("delta position", deltaX +  " / " + deltaY + " / " + hero.position.x + " / " + hero.position.y);
	    }
		if (!_dragging){
			// Snap back to center when the joystick is released
			_touchingPoint.x = x;
			_touchingPoint.y = y;
			//shaft.alpha = 0;
		}
	}

	
}
